#include "programDownloadProgressMessage.h"
#include "jsonUtils.h"

ProgramDownloadProgressMessage::ProgramDownloadProgressMessage(MtpProtocol* packet) : MtpMessage(packet), status(0), percent(0)
{

}

ProgramDownloadProgressMessage::ProgramDownloadProgressMessage(const int& status, const int& percent, const String& message)
	: MtpMessage(new MtpProtocol(L"programDownloadProgress")), status(status), percent(percent), message(message)
{
}

ProgramDownloadProgressMessage::ProgramDownloadProgressMessage(const int& status, const int& percent)
	: MtpMessage(new MtpProtocol(L"programDownloadProgress")), status(status), percent(percent)
{
}

void ProgramDownloadProgressMessage::decodeBody()
{
	MtpMessage::decodeBody();
	if (!jsonContent.empty()) {
		JsonDoc doc(rapidjson::kObjectType);
		doc.Parse(jsonContent.c_str());
		JsonUtils::loadInt(doc, L"status", status);
		JsonUtils::loadInt(doc, L"percent", percent);
		JsonUtils::loadStr(doc, L"message", message);
	}
}

void ProgramDownloadProgressMessage::encodeBody()
{
	JsonDoc doc(rapidjson::kObjectType);
	doc.AddMember(L"status", status, doc.GetAllocator());
	doc.AddMember(L"percent", percent, doc.GetAllocator());
	JsonUtils::addString(doc, L"message", message, doc.GetAllocator());
	jsonContent = JsonUtils::jsonStr(doc);
	MtpMessage::encodeBody();
}
